/*
 * @Description:
 * @Version: 1.0
 * @Autor: lzc
 * @Date: 2021-06-28 14:17:27
 * @LastEditors: lzc
 * @LastEditTime: 2021-07-29 13:03:01
 */
#include <rtthread.h>
#include <rtdevice.h>
#include "drv_gpio.h"
#include "startup.h"
#include "my_elcdif.h"
#include "GUI_Builder.h"
#include "Resource.h"
#include "watchdog.h"
#include "at24cxx.h"
#include <stdio.h>
#include "GUI.h"
#include "WM.h"
#define WDT_DEVICE_NAME "wdog1" /* 看门狗设备名称 */
#define IIC_DEVICE_NAME "i2c1"  /* IIC设备名称 */
static rt_thread_t emwin = RT_NULL; 
WM_Status_t WM_Status;

static rt_device_t wdg_dev; /* 看门狗设备句柄 */

/**
 * @function: idle_hook
 * @brief: 空闲钩子函数
 * @param {*}
 * @return {*}
 * @author: lzc
 */
static void idle_hook(void)
{
    /* 在空闲线程的回调函数里喂狗 */
    rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
    rt_kprintf("feed the dog!\n ");
}
/**
 * @function: wdt_sample
 * @brief: 看门狗例子
 * @param {int} argc
 * @param {char} argv
 * @return {*}
 * @author: lzc
 */
static int wdt_sample(int argc, char argv[])
{
    rt_err_t ret = RT_EOK;
    rt_uint32_t timeout = 1; /* 溢出时间，单位：秒 */
    char device_name[RT_NAME_MAX] = WDT_DEVICE_NAME ;

    /* 根据设备名称查找看门狗设备，获取设备句柄 */
    wdg_dev = rt_device_find(device_name);
    if (!wdg_dev)
    {
        rt_kprintf("find %s failed!\n", device_name);
        return RT_ERROR;
    }
    /* 初始化设备 */
    ret = rt_device_init(wdg_dev);
    if (ret != RT_EOK)
    {
        rt_kprintf("initialize %s failed!\n", device_name);
        return RT_ERROR;
    }
    /* 设置看门狗溢出时间 */
    ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
    if (ret != RT_EOK)
    {
        rt_kprintf("set %s timeout failed!\n", device_name);
        return RT_ERROR;
    }
    /* 启动看门狗 */
    ret = rt_device_control(wdg_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL);
    if (ret != RT_EOK)
    {
        rt_kprintf("start %s failed!\n", device_name);
        return -RT_ERROR;
    }
    /* 设置空闲线程回调函数 */
    rt_thread_idle_sethook(idle_hook);

    return ret;
}
MSH_CMD_EXPORT(wdt_sample, wdt Thread);

static void HAL_PowerOn_Value_Init(void)
{
    // 默认窗口是ID_SCREEN_00
    WM_Status.Current_Screen = ID_SCREEN_00; 
}

static void HAL_PowerOn_Function_Init(void)
{
    // 初始化IIC的EEPROM
    at24cxx_init(IIC_DEVICE_NAME, 0);
}

static void emwinapp_task(void *pvParameters)
{
    //rt_kprintf("\r\n[emwin]step1\r\n");
    WM_MULTIBUF_Enable(1);
    //rt_kprintf("\r\n[emwin]step2\r\n");
    MainTask();
}
static void startup_task(void *pvParameters)
{
    emwin = rt_thread_create("emwin", emwinapp_task, RT_NULL, 1024 * 16, 30, 10);
    if (emwin != RT_NULL)
        rt_thread_startup(emwin);
    else
        rt_kprintf("creat emwin failed\r\n");
    rt_thread_mdelay(100);
}
static int startup_task_create(void)
{
    int result = RT_EOK;
    HAL_PowerOn_Value_Init();
    HAL_PowerOn_Function_Init();
    // rt_thread_t thread_startup = rt_thread_create("startup_task", startup_task, RT_NULL, 1024 * 16, 25, 10);
    // rt_thread_startup(thread_startup);
    return result;
}
INIT_APP_EXPORT(startup_task_create);
